package cn.yyh.serviceOrg.service.impl;

import cn.yyh.commons.service.impl.MyBaseServiceImpl;
import cn.yyh.serviceOrg.entity.Department;
import cn.yyh.serviceOrg.mapper.DepartmentMapper;
import cn.yyh.serviceOrg.service.DepartmentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-07-10
 */
@Service
public class DepartmentServiceImpl extends MyBaseServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public void save(Department department) {
        baseMapper.save(department);
        // 因为添加和修改时都要给部门动态生成dirPath路径,所以调用修改方法
        this.update(department);
    }

    @Override
    public void update(Department department) {
        // 当上级部门的id为空,则表示现在的对象是顶级部门,直接拼接自己部门的id
        if (department.getParent() == null || department.getParent().getId() == null) {

            department.setDirPath("/" + department.getId());
        }else {
            // 不为空则表示有上级部门,获取到上级部门的dirPath,和现在部门的id拼接
            Department byId = baseMapper.getById(department.getParent().getId());
            department.setDirPath(byId.getDirPath() + "/" + department.getId());
        }

        baseMapper.update(department);
    }

    @Override
    public List<Department> listDeptTree(Long id) {
        return baseMapper.listDeptTree(id);
    }

}
